const { count } = require('console');
const express = require('express');
//引入连接池模块
const pool = require('../pool');
// 创建路由器对象
const router = express.Router();
// 注册路由
router.post('/register', (req, res) => {
    let obj = req.body;
    // 验证各项不为空
    let i = 401;
    for (var k in obj) {
        i++;
        if (!obj[k]) {
            res.send({ code: i, msg: k + '不能为空' });
            return;
        }
    }
    // 验证用户名是否存在
    // pool.query('SELECT uname from xz_user WHERE uname=?', [obj.uname], (err, result) => {
        // 如果存在
        // if (result.length) {
            // res.send({ code: 201, msg: '用户名已经存在' });
        // } else {
            // if(err){res.send({code:500,msg:'服务器端错误'});}
            // 如果不存在添加用户
            let sql = 'INSERT INTO xz_user set ?';
            pool.query(sql, [obj], (err, result) => {
                if (err) {res.send({code:500,msg:'服务器端错误'})};
                res.send({ code: 200, msg: '用户添加成功' });
            });
        // }
    // });
});
// 登录路由
router.post('/login', (req, res) => {
    let obj = req.body;
    let code = 400;
    for (var k in obj) {
        code++;
        if (!obj[k]) {
            res.send({ code: code, msg: k + "不能为空" });
            return;
        }
    }
    // 查询数据库中是否有对应的用户名和密码
    let sql = 'SELECT * FROM xz_user WHERE uname=? AND upwd=?';
    pool.query(sql, [obj.uname, obj.upwd], (err, result) => {
        if (err) {
            res.send({
                code: 500,
                msg: '服务器端错误'
            });
            return;
        }
        if (!result.length) {
            res.send({ code: 201, msg: '用户名或密码错误' });
        } else {
            res.send({ code: 200, msg: '登录成功' });
        }
    });
});
// 修改用户
router.put('/update/:uid', (req, res) => {
    let $uid = req.params.uid;
    let obj = req.body;
    let code = 400;
    for (var k in obj) {
        code++;
        if (!obj[k]) {
            res.send({ code: code, msg: k + '不能为空' });
            return;
        }
    }
    // 执行sql语句
    let sql = 'UPDATE xz_user SET ? WHERE uid = ?';
    pool.query(sql, [obj, $uid], (err, result) => {
        if (err) {
            res.send({
                code: 500,
                msg: '服务器端错误'
            })
            return;
        }
        console.log(result);
        if (!result.affectedRows) {
            res.send({ code: 201, mas: '修改失败' });
        } else {
            res.send({ code: 200, mas: '修改成功' });
        }
    });
});
// 查询用户列表
router.get('/list', (req, res) => {
    // 后台获取的数据是字符串形式的
    console.log(req.query);
    let $pno = 0;
    let $count = 0
    // 页码默认值为1
    !req.query.pno ? $pno = 1 : $pno = parseInt(req.query.pno);
    // 数量默认值为2
    !req.query.count ? $count = 2 : $count = parseInt(req.query.count);
    let $start = ($pno - 1) * $count;
    // 执行sql语句
    let sql = 'SELECT uname,email,gender,phone FROM xz_user LIMIT ?,?';
    pool.query(sql, [$start, $count], (err, result) => {
        console.log(result);
        if (err) {
            res.send({ code: 500, msg: '服务器端错误' });
            return;
        };
        !result.length ? res.send({ code: 201, msg: '没有更多内容了' }) : res.send({ code: 200, msg: '查询成功', data: result });
    });
});
// 在user.js中，添加检测用户名是否存在的路由(get  /checkuname)，以查询字符串形式传递用户名，在路由中获取数据，如果用户名已经存在响应{code:201,msg:'该用户已注册'}，否则{code: 200,msg:'该用户可以使用'}
// 检测用户名是否存在
router.get('/checkuname',(req,res)=>{
    let obj = req.query;
    pool.query('SELECT uname from xz_user WHERE uname=?', [obj.uname], (err, result) => {
        if(err){
            res.send({ code: 500, msg: '服务器端错误' });
            return;
        };
        //  如果存在
        result.length?res.send({ code: 201, msg: '用户名已经存在' }):res.send({ code: 200, msg: '该用户可以使用' });
        
    })
});
// 删除用户(delete  /编号)
router.post('/delete/:uid',(req,res)=>{
    let $uid = req.params.uid;
    let sql = 'DELETE FROM xz_user WHERE uid = ?';
    pool.query(sql,[$uid],(err,result)=>{
        if(err){throw err};
        if(!result.affectedRows){
            res.send({code:201,msg:'用户不存在'});
        }else{
            res.send({code:200,msg:'删除成功'});
        }
    });
});
// 根据id查找指定用户
router.get('/search/:uid',(req,res)=>{
    let $uid = req.params.uid;
    let sql = 'SELECT uname,email FROM xz_user WHERE uid = ?';
    pool.query(sql,[$uid],(err,result)=>{
        if(err){
            res.send({ code: 500, msg: '服务器端错误' });
            return;
        };
        if(!result.length){
            res.send({code:201,msg:'查无此人'});
        }else{
            res.send({code:200,msg:'查询成功',data:result});
        }
    });
})
// 导出路由器模块
module.exports = router;
// uid | uname    | upwd   | email       | phone       | avatar                 | user_name | genderz
